/**
 * Copyright (C) 2012 Daniel Gee.
 * See LICENSE.txt for more details.
 */
package examples;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import com.rl.dungeon.Location;
import com.rl.dungeon.builders.DefaultBuilder;
import com.rl.dungeon.pathing.HillMap;
import com.rl.gui.ScreenPanel;
import com.rl.util.DiceRoller;

/**
 * Simple example of {@link HillMap} doing its thing. Uses {@link ScreenPanel} for display.
 * 
 * @author Daniel Gee
 */
@SuppressWarnings("serial")
public class HillMapSample extends JFrame
{
	/**
	 * 25 chars tall.
	 */
	public static final int DUNGEON_HEIGHT = 25;

	/**
	 * 80 chars wide
	 */
	public static final int DUNGEON_WIDTH = 80;

	/**
	 * 
	 * @param args
	 *            none are read.
	 */
	public static void main(String... args)
	{
		Location.setCacheEdge(Location
				.of(HillMapSample.DUNGEON_WIDTH, HillMapSample.DUNGEON_HEIGHT));

		EventQueue.invokeLater(new Runnable()
		{
			@Override
			public void run()
			{
				try
				{
					HillMapSample frame = new HillMapSample();
					frame.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public HillMapSample()
	{
		// Self setup.
		this.setTitle("Hill Map Sample: Uses a HillMap to figure a path (click a cell)");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension currentResolution = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(currentResolution.width / 5, currentResolution.height / 4);
		this.setSize(655, 515);

		// A JMenubar along the top.
		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		JMenuItem mntmQuit = new JMenuItem("Quit");
		mntmQuit.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		mnFile.add(mntmQuit);

		// Things we'll be showing.
		final ScreenPanel screenPanel = new ScreenPanel();
		screenPanel.setFont(new Font("Courier New", Font.PLAIN, 14));
		screenPanel.setLineOffset(0);

		final JLabel locationLabel = new JLabel("Location");
		final JLabel infoLabel = new JLabel("Info");
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(locationLabel, BorderLayout.NORTH);
		this.getContentPane().add(screenPanel, BorderLayout.CENTER);
		this.getContentPane().add(infoLabel, BorderLayout.SOUTH);

		final DefaultBuilder dg = new DefaultBuilder(new DiceRoller());
		final SimpleDungeon theDungeon = new SimpleDungeon(dg.generate(HillMapSample.DUNGEON_WIDTH,
				HillMapSample.DUNGEON_HEIGHT));
		screenPanel.setScreen(new PathFilteredScreen(null, theDungeon));

		/**
		 * We'll show some DEBUG info on top of the normal input processing that ScreenPanel does.
		 */
		screenPanel.addMouseMotionListener(new MouseMotionListener()
		{

			@Override
			public void mouseDragged(MouseEvent e)
			{

			}

			@Override
			public void mouseMoved(MouseEvent e)
			{
				Location loc = screenPanel.locationOf(e.getX(), e.getY());
				locationLabel.setText("Mouse At: " + loc);
			}
		});
		screenPanel.addMouseListener(new MouseListener()
		{

			@Override
			public void mouseClicked(MouseEvent e)
			{

			}

			@Override
			public void mouseEntered(MouseEvent e)
			{

			}

			@Override
			public void mouseExited(MouseEvent e)
			{

			}

			@Override
			public void mousePressed(MouseEvent e)
			{
				final Location start = screenPanel.locationOf(e.getX(), e.getY());

				/**
				 * This line is potentially sketchy because it relies on the specific ordering of
				 * the Listeners so that the PathFilteredScreen will have regenerated it's path by
				 * the time the event causes this code to run.
				 */
				final List<Location> path = ((PathFilteredScreen) screenPanel.getScreen())
						.getPath();

				infoLabel.setText("Clicked Cell: " + start + ", R-Dist: "
						+ start.rogueDistance(theDungeon.getStairs())
						+ (path != null ? ", Path Dist: " + path.size() : ""));
			}

			@Override
			public void mouseReleased(MouseEvent e)
			{

			}
		});
	}
}
